Some of the Linux PCI functions called by the virtual configuration
authorkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Tue, 25 Apr 2006 17:13:39 +0000 (18:13 +0100)
committerkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Tue, 25 Apr 2006 17:13:39 +0000 (18:13 +0100)
space handlers were making calls into ACPI code which uses semaphores.
Since semaphores can not be locked while atomic (because they could
sleep), I changed the way the PCI backend responds to requests from the
frontend. Previously, the virtual configuration space handlers ran in
the same context as the event channel interrupt handler (which was often
atomic if not always atomic). Now the interrupt handler schedules a
callback function (a bottom half) in the system work queue (keventd)
that will get called in process context at a slightly later time. This
allows the handlers in the virtual configuration space to run in process
context and to call any core PCI function regardless of whether it will
sleep or not.

Signed-off-by: Ryan Wilson <hap9@epoch.ncsc.mil>
linux-2.6-xen-sparse/drivers/xen/pciback/pciback.h
linux-2.6-xen-sparse/drivers/xen/pciback/pciback_ops.c
linux-2.6-xen-sparse/drivers/xen/pciback/xenbus.c

index 77d63e0030e848fc3217496d30300838ed706510..09deb26522a5f90273a88de8bb87861297187461 100644 (file)
@@ -11,6 +11,8 @@
 #include <xen/xenbus.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
+#include <linux/workqueue.h>
+#include <asm/atomic.h>
 #include <xen/interface/io/pciif.h>
 
 struct pci_dev_entry {
@@ -18,6 +20,9 @@ struct pci_dev_entry {
        struct pci_dev *dev;
 };
 
+#define _PDEVF_op_active       (0)
+#define PDEVF_op_active        (1<<(_PDEVF_op_active))
+
 struct pciback_device {
        void *pci_dev_data;
        spinlock_t dev_lock;
@@ -31,6 +36,10 @@ struct pciback_device {
 
        struct vm_struct *sh_area;
        struct xen_pci_sharedinfo *sh_info;
+
+       unsigned long flags;
+
+       struct work_struct op_work;
 };
 
 struct pciback_dev_data {
@@ -71,6 +80,7 @@ void pciback_release_devices(struct pciback_device *pdev);
 
 /* Handles events from front-end */
 irqreturn_t pciback_handle_event(int irq, void *dev_id, struct pt_regs *regs);
+void pciback_do_op(void *data);
 
 int pciback_xenbus_register(void);
 void pciback_xenbus_unregister(void);
index 48305f549d9579a21ed790faec6d4cc57b54dca6..4dc35d581ceb0f096ffc3c1fb41b86d85816147c 100644 (file)
@@ -40,18 +40,25 @@ void pciback_reset_device(struct pci_dev *dev)
        pciback_config_reset(dev);
 }
 
-irqreturn_t pciback_handle_event(int irq, void *dev_id, struct pt_regs *regs)
+static inline void test_and_schedule_op(struct pciback_device *pdev)
 {
-       struct pciback_device *pdev = dev_id;
+       /* Check that frontend is requesting an operation and that we are not
+        * already processing a request */
+       if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
+           && !test_and_set_bit(_PDEVF_op_active, &pdev->flags))
+               schedule_work(&pdev->op_work);
+}
+
+/* Performing the configuration space reads/writes must not be done in atomic
+ * context because some of the pci_* functions can sleep (mostly due to ACPI
+ * use of semaphores). This function is intended to be called from a work
+ * queue in process context taking a struct pciback_device as a parameter */
+void pciback_do_op(void *data)
+{
+       struct pciback_device *pdev = data;
        struct pci_dev *dev;
        struct xen_pci_op *op = &pdev->sh_info->op;
 
-       if (unlikely(!test_bit(_XEN_PCIF_active,
-                              (unsigned long *)&pdev->sh_info->flags))) {
-               pr_debug("pciback: interrupt, but no active operation\n");
-               goto out;
-       }
-
        dev = pciback_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
 
        if (dev == NULL)
@@ -65,10 +72,25 @@ irqreturn_t pciback_handle_event(int irq, void *dev_id, struct pt_regs *regs)
        else
                op->err = XEN_PCI_ERR_not_implemented;
 
+       /* Tell the driver domain that we're done. */ 
        wmb();
        clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
        notify_remote_via_irq(pdev->evtchn_irq);
 
-      out:
+       /* Mark that we're done */
+       wmb();
+       clear_bit(_PDEVF_op_active, &pdev->flags);
+
+       /* Check to see if the driver domain tried to start another request
+        * in between clearing _XEN_PCIF_active and clearing _PDEVF_op_active */
+       test_and_schedule_op(pdev);
+}
+
+irqreturn_t pciback_handle_event(int irq, void *dev_id, struct pt_regs *regs)
+{
+       struct pciback_device *pdev = dev_id;
+
+       test_and_schedule_op(pdev);
+
        return IRQ_HANDLED;
 }
index f073b35679ae3c84ac8fd22090001b8266e5a0b5..28074c34e87382b58d463110b94d641311662a06 100644 (file)
@@ -31,6 +31,8 @@ static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
        pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
        pdev->be_watching = 0;
 
+       INIT_WORK(&pdev->op_work, pciback_do_op, pdev);
+
        if (pciback_init_devices(pdev)) {
                kfree(pdev);
                pdev = NULL;
@@ -48,6 +50,11 @@ static void free_pdev(struct pciback_device *pdev)
        if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ)
                unbind_from_irqhandler(pdev->evtchn_irq, pdev);
 
+       /* If the driver domain started an op, make sure we complete it or
+        * delete it before releasing the shared memory */
+       cancel_delayed_work(&pdev->op_work);
+       flush_scheduled_work();
+
        if (pdev->sh_info)
                xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_area);